package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateTime;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import jodd.util.StringUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

/**
 * @BelongsProject: tingshu-parent
 * @BelongsPackage: com.atguigu.tingshu.album.service.impl
 * @Author: jiangyi
 * @CreateTime: 2025-09-25  23:05
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConstantProperties minioProperties;


    @Override
    public String ImageFileUpload(MultipartFile file)  {
        try {
            // IO流判断是否为图片文件
            InputStream inputStreamImage = file.getInputStream();
            BufferedImage bufferedImage = ImageIO.read(inputStreamImage);
            if (bufferedImage == null) {
                throw new GuiguException(400, "上传文件格式错误");
            }

            // 创建上传Minio文件夹层级结构
            String dateName = new DateTime().toString("yyyy/MM/dd");
            String originalFilename = file.getOriginalFilename();
            // 获取文件后缀
            String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
            // 创建上传Minio文件名
            String name = UUID.randomUUID().toString().replaceAll("-", "");
            String fileName = name + "." + filenameExtension;

            // 上传Minio
            InputStream inputStream = file.getInputStream();

            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .object(dateName + "/" + fileName)
                    .stream(inputStream, inputStream.available(), -1)
                    .contentType(file.getContentType())
                    .build());

            // http://192.168.254.156:9001/browser/glsx/MjAyNS8wOS8yOS8=
            return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + dateName + "/" + fileName;
        } catch (IOException e) {
            throw new GuiguException(400, "文件读取失败: " + e.getMessage());
        } catch (Exception e) {
            // 统一处理所有Minio相关异常
            throw new GuiguException(20001, "文件上传失败: " + e.getMessage());
        }
    }
}
